package edu.uoc.tds.pac4.client.ss4;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import edu.uoc.tds.pac4.beans.Event;
import edu.uoc.tds.pac4.beans.Participant;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.client.ss4.GestorPantallaSS4.TipusPantalla;
import edu.uoc.tds.pac4.client.ss4.GestorPantallaSS4;
import edu.uoc.tds.pac4.client.ss4.JPanelLlistatEvents;
import edu.uoc.tds.pac4.client.ss4.JPanelFiltreConsultaEvents;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

public class PantallaConsultaEvents extends PantallaInscripcions implements PanelClientInterface {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;
	private JPanelFiltreConsultaEvents jPanelFiltre = null;
	private JPanelLlistatEvents jPanelEvents = null;
	private JPanel jPanelInferior = null;
	private JButton jButtonDetalls = null;
	private Vector<Event> currentEvents = null;
	private JLabel jLabelUsuari = null;
	private JLabel jLabelFieldUsuari = null;
	private GestorPantallaSS4 gestor;
	private Participant usuari = null;
	private JPanel jPanelTitol = null;
	private JLabel jLabelTitolPantalla = null;	
	
	/**
	 * Constructor
	 */
	public PantallaConsultaEvents() {
		super();
		initialize();
	}

	/**
	 * Constructor que rep una referencia al servidor remot i client
	 * @param client  
	 * @param remot
	 */
	public PantallaConsultaEvents(RemoteInterface remot, GestorPantallaSS4 gestor) {
		this.remot = remot;
		this.gestor = gestor;
		initialize();
	}

	
	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize() {
		usuari = gestor.getParticipant();
	}

	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
						
			// Titol
			GridBagConstraints gbcPanelTitol = new GridBagConstraints();
			gbcPanelTitol.gridx = 0;
			gbcPanelTitol.gridy = 0;
			gbcPanelTitol.gridwidth = 3;
			gbcPanelTitol.gridheight = 3;
			gbcPanelTitol.fill = GridBagConstraints.HORIZONTAL;
			gbcPanelTitol.anchor = GridBagConstraints.PAGE_START;
			gbcPanelTitol.insets = new Insets (10, 10, 10, 10);
					
			
			//Filtre
			GridBagConstraints gbcPanelFiltre = new GridBagConstraints();
			gbcPanelFiltre.gridx = 0;
			gbcPanelFiltre.gridy = 4;
			
			//Llistat Events
			GridBagConstraints gbcPanelEvents = new GridBagConstraints();
			gbcPanelEvents.gridx = 0;
			gbcPanelEvents.gridy = 5;
			gbcPanelEvents.weightx = 1;
			gbcPanelEvents.weighty = 1;
			gbcPanelEvents.insets = new Insets(0, 10, 10, 10);
			gbcPanelEvents.fill = GridBagConstraints.BOTH;		
			
			//Panel Inferior
			GridBagConstraints gbcPanelButton = new GridBagConstraints();
			gbcPanelButton.gridx = 0;
			gbcPanelButton.gridy = 6;
			gbcPanelButton.insets = new Insets(0, 10, 10, 10);
						
			jPanelContainer = new JPanel();
			jPanelContainer.setSize(dimension);
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.add(getJPanelTitol(), gbcPanelTitol);
			jPanelContainer.add(getJPanelFiltre(), gbcPanelFiltre);
			jPanelContainer.add(getJPanelEvents(), gbcPanelEvents);	
			jPanelContainer.add(getJPanelInferior(), gbcPanelButton);
		}
		return jPanelContainer;
	}
	
	/**
	 * This method initializes jPanelTitol
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelTitol() {
		if (jPanelTitol == null) {
			Insets titleInset = new Insets(10, 15, 10, 10);
			Insets textInset = new Insets(10, 5, 10, 25);
			
			// Usuari Participant
			GridBagConstraints gbcLabelUsuari = new GridBagConstraints();
			gbcLabelUsuari.gridx = 2;
			gbcLabelUsuari.gridy = 0;
			gbcLabelUsuari.insets = titleInset;
			jLabelUsuari = new JLabel();		
			jLabelUsuari.setText(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Usuari"));
			GridBagConstraints gbcTextUsuari = new GridBagConstraints();
			gbcTextUsuari.gridx = 3;
			gbcTextUsuari.gridy = 0;
			gbcTextUsuari.insets = textInset;
			jLabelFieldUsuari = new JLabel();
			jLabelFieldUsuari.setText(usuari.getNom()+" "+usuari.getCognoms()+ "     " );
	
			jLabelTitolPantalla = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Titol"));
			GridBagConstraints gbcTitolUsuari = new GridBagConstraints();
			gbcTitolUsuari.gridx = 1;
			gbcTitolUsuari.gridy = 0;
			gbcTitolUsuari.insets = titleInset;

			jPanelTitol = new JPanel();
			jPanelTitol.setLayout(new GridBagLayout());
			jPanelTitol.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			jPanelTitol.add(jLabelTitolPantalla, gbcTitolUsuari);
			jPanelTitol.add(jLabelUsuari, gbcLabelUsuari);
			jPanelTitol.add(jLabelFieldUsuari, gbcTextUsuari);
		}
		return jPanelTitol;
	}

	

	/**
	 * This method initializes jPanelFiltre
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelFiltre() {
		if (jPanelFiltre == null) {
			jPanelFiltre = new JPanelFiltreConsultaEvents(this);
		}
		return jPanelFiltre;
	}

	/**
	 * This method initializes jPanelEvents
	 * 
	 * @return JPanelEvents
	 */
	private JPanelLlistatEvents getJPanelEvents() {
		if (jPanelEvents == null) {
			jPanelEvents = new JPanelLlistatEvents();
		}
		return jPanelEvents;
	}
	
	private JPanel getJPanelInferior() {
		if (jPanelInferior == null) {
			jPanelInferior = new JPanel();
			jPanelInferior.setLayout(new GridBagLayout());
					
			GridBagConstraints gbcButtonDetalls = new GridBagConstraints();
			gbcButtonDetalls.gridx = 1;
			gbcButtonDetalls.insets = new Insets(0, 10, 10, 10);
	
			jPanelInferior.add(getJButtonDetalls(), gbcButtonDetalls);

		}
		return jPanelInferior;
	}
	
	private JButton getJButtonDetalls() {
		if(jButtonDetalls == null) {
			jButtonDetalls = new JButton();
			jButtonDetalls = new JButton(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Detalls"));
			jButtonDetalls.setSize(200, 20);
			jButtonDetalls.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int i = jPanelEvents.getSelectedRow();
					if(i >= 0) {
						//obtenim codiEvent de la fila seleccionada
						gestor.carregaPantalla(TipusPantalla.inscripcioEvent, currentEvents.get(i));												
					}
					else
						new PantallaError("Client.PantallaConsultaEvents.SeleccioEvent", PantallaError.Tipus.WARNING);
				}
			});
		}
		return jButtonDetalls;
	}
	
	/**
	 * Actualitza el contingut de la taula
	 */
	public void updateTaula() {		
		Date data = jPanelFiltre.getData();				
		// crida al servidor per rebre els events
		currentEvents = null;		
		try {
			currentEvents = remot.getEvents(jPanelFiltre.getTipusEvent(), data, true);
		} catch (RemoteException e) {
			new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
		} catch (GestioEventsException e) {
			new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
		}
		jPanelEvents.drawEvents(currentEvents);		
	}
}
